﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    [Range(1,10)]
    [SerializeField] private float moveSpeed;

    private Vector2 target;


    private void Awake ()
    {
        //sr = GetComponent<SpriteRenderer> ();
    }

    public void ResetRote ()
    {
        
    }

    //private bool isTouch = false;
    bool isRoteR = false;
    bool isRoteL = false;
    // Update is called once per frame
    void Update()
    {
        if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        {
            OnPlayerMove (-1);
            if (!isRoteR && !isRoteL)
            {
                isRoteL = true;
            }
            if (!isRoteL)
            {
                transform.Rotate (new Vector3 (0, -180, 0));
                isRoteL = true;
                isRoteR = false;
            }
            
        }
        else if (Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.RightArrow))
        {
            if (!isRoteR && !isRoteL)
            {
                transform.Rotate (new Vector3 (0, 180, 0));
                isRoteR = true;
            }
            OnPlayerMove (1);
            if (!isRoteR)
            {
                transform.Rotate (new Vector3 (0, 180, 0));
                isRoteR = true;
                isRoteL = false;
            }
        }
    }

    private void OnPlayerMove (int speed)
    {
        //if (isTouch) return;
        target = transform.position;
        target.x += moveSpeed * Time.deltaTime * speed;
        transform.position = target;
    }
}

public class MousePos
{
    static Vector3 mousePositionOnScreen;//获取到点击屏幕的屏幕坐标
    static Vector3 mousePositionInWorld; //将点击屏幕的屏幕坐标转换为世界坐标
    public static Vector2 MouseFollow ()
    {
        mousePositionOnScreen = Input.mousePosition;
        mousePositionInWorld = Camera.main.ScreenToWorldPoint (mousePositionOnScreen);
        return new Vector2 (mousePositionInWorld.x, mousePositionInWorld.y);
    }
}

public class Jugde
{
    public static bool Contain(Vector2 pos1, Vector2 pos2, float sub = 0.05f)
    {
        if (pos1.x + sub >= pos2.x && pos1.x - sub <= pos2.x) return false;
        if (pos1.y + sub >= pos2.y && pos1.y - sub <= pos2.y) return false;
        return true;
    }

    public static bool Contain(float f1, float f2, float sub = 0.05f)
    {
        if (f1 + 0.05f >= f2 && f1 - 0.05f <= f2) return false;

        return true;
    }
}